package io.netty.example.test.server;

import com.alibaba.fastjson.JSONObject;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.example.test.client.RequestFuture;
import io.netty.example.test.client.Response;

public class ServerHandler extends ChannelInboundHandlerAdapter {
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
//        if (msg instanceof ByteBuffer) {
//            System.out.println(((ByteBuffer) msg).toString());
//        }
//        ctx.channel().writeAndFlush("server received");
        RequestFuture requestFuture = JSONObject.parseObject(((String) msg), RequestFuture.class);
        Response response = new Response();
        response.setId(requestFuture.getId());
        response.setResult("received id:" + requestFuture.getId());
        System.out.println("received id:" + requestFuture.getId());
        ctx.channel().writeAndFlush(JSONObject.toJSONString(response));
    }
}
